<template>
	<view class="box">
		<u--form labelPosition="left" :model="userInfo" :rules="rules" ref="form1" label-align="left">
			<u-form-item label="旧密码" prop="oldPassword" borderBottom label-width="120">
				<u--input v-model="userInfo.oldPassword" border="none" password placeholder="请输入旧密码">
				</u--input>
			</u-form-item>
			<u-form-item label="新密码" prop="newPassword" borderBottom label-width="120">
				<u--input v-model="userInfo.newPassword" border="none" password placeholder="请输入新密码">
				</u--input>
			</u-form-item>
			<u-form-item label="确认密码" prop="newPasswords" borderBottom label-width="120">
				<u--input v-model="userInfo.newPasswords" border="none" password placeholder="请再次输入新密码">
				</u--input>
			</u-form-item>
		</u--form>
		<u-button type="primary" text="确认修改" color="#2289FF" class="btnB" @click="sbumit"></u-button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				rules: {
					oldPassword: {
						type: 'string',
						required: true,
						message: '请输入旧密码',
						trigger: ['blur']
					},
					newPassword: {
						type: 'string',
						required: true,
						message: '请输入新密码',
						trigger: ['blur']
					},
					newPasswords: {
						type: 'string',
						required: true,
						message: '请再次输入新密码',
						trigger: ['blur']
					},
				},
				userInfo: {
					oldPassword: '', //旧密码
					newPassword: '', //新密码
					newPasswords: '', //确认新密码
				},
			}
		},
		methods: {
			// 确认
			sbumit() {
				this.$refs.form1.validate().then(res => {
					if (this.userInfo.newPassword === this.userInfo.newPasswords) {
						let data =
							`?oldPassword=${this.userInfo.oldPassword}&newPassword=${this.userInfo.newPassword}`
						this.$api.updatePwd({}, data).then(res => {
							uni.showModal({
								title: '',
								content: res.code ? '修改成功！将跳转登录页！' : '修改失败！',
								showCancel: false,
								success: function(v) {
									if (v.confirm) {
										if (res.code != 200) return
										uni.reLaunch({
											url: '/pagesA/public/login'
										})
									}
								}
							});
						})
					} else {
						uni.showToast({
							title: '新密码和确认密码不一致',
							duration: 2000,
							icon: 'none',
							duration: 2000,
						});
					}
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.box {
		padding: 5rpx 28rpx;
	}

	::v-deep .u-button {
		position: fixed !important;
		bottom: 111rpx !important;
		// left: 10rpx;
		// right: 10rpx;
		width: 694rpx !important;
		height: 92rpx !important;
		font-size: 28rpx !important;
	}
</style>
